{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a88ed094-8bda-4356-8912-a8b6feb4a76f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入记录已添加：2023-04-01 - 5000元 - 工资 - 本月工资\n",
      "支出记录已添加：2023-04-02 - 1000元 - 餐饮 - 晚餐\n",
      "支出记录已添加：2023-04-03 - 2000元 - 购物 - 购买新衣\n",
      "收入记录：\n",
      "2023-04-01 - 5000元 - 工资 - 本月工资\n",
      "支出记录：\n",
      "2023-04-02 - 1000元 - 餐饮 - 晚餐\n",
      "2023-04-03 - 2000元 - 购物 - 购买新衣\n",
      "当前余额：2000元\n"
     ]
    }
   ],
   "source": [
    "class BillManager:  \n",
    "    def __init__(self):  \n",
    "        self.income_records = []  \n",
    "        self.expense_records = []  \n",
    "  \n",
    "    def add_income(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"收入金额必须为正数！\")  \n",
    "            return  \n",
    "        self.income_records.append({  \n",
    "            'date': date,  \n",
    "            'amount': amount,  \n",
    "            'category': category,  \n",
    "            'note': note  \n",
    "        })  \n",
    "        print(f\"收入记录已添加：{date} - {amount}元 - {category} - {note}\")  \n",
    "  \n",
    "    def add_expense(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"支出金额必须为正数！\")  \n",
    "            return  \n",
    "        self.expense_records.append({  \n",
    "            'date': date,  \n",
    "            'amount': amount,  \n",
    "            'category': category,  \n",
    "            'note': note  \n",
    "        })  \n",
    "        print(f\"支出记录已添加：{date} - {amount}元 - {category} - {note}\")  \n",
    "  \n",
    "    def list_incomes(self):  \n",
    "        if not self.income_records:  \n",
    "            print(\"没有收入记录。\")  \n",
    "            return  \n",
    "        print(\"收入记录：\")  \n",
    "        for record in self.income_records:  \n",
    "            print(f\"{record['date']} - {record['amount']}元 - {record['category']} - {record['note']}\")  \n",
    "  \n",
    "    def list_expenses(self):  \n",
    "        if not self.expense_records:  \n",
    "            print(\"没有支出记录。\")  \n",
    "            return  \n",
    "        print(\"支出记录：\")  \n",
    "        for record in self.expense_records:  \n",
    "            print(f\"{record['date']} - {record['amount']}元 - {record['category']} - {record['note']}\")  \n",
    "  \n",
    "    def calculate_balance(self):  \n",
    "        total_income = sum(record['amount'] for record in self.income_records)  \n",
    "        total_expense = sum(record['amount'] for record in self.expense_records)  \n",
    "        balance = total_income - total_expense  \n",
    "        print(f\"当前余额：{balance}元\")  \n",
    "  \n",
    "# 使用示例  \n",
    "bm = BillManager()  \n",
    "bm.add_income(\"2023-04-01\", 5000, \"工资\", \"本月工资\")  \n",
    "bm.add_expense(\"2023-04-02\", 1000, \"餐饮\", \"晚餐\")  \n",
    "bm.add_expense(\"2023-04-03\", 2000, \"购物\", \"购买新衣\")  \n",
    "  \n",
    "bm.list_incomes()  \n",
    "bm.list_expenses()  \n",
    "bm.calculate_balance()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bd185a4-c006-4e0d-84ac-5c2f0a064b09",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
